package com.DuoXianCheng.T04;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Random;

public class C01 extends Thread {
    // 共享数据
    static double money = 100;
    static int count = 3;
    static final double Min = 0.01;
    
    @Override
    public void run() {
        Random r = new Random();
        synchronized (C01.class) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            if (count == 0) {
                System.out.println(getName() + "没有抢到！");
            } else {
                double price = 0;
                if (count == 1) {
                    price = money;
                } else {
                    double bounds = money - (count - 1) * Min;
                    double num = r.nextDouble() * money;
                    BigDecimal bd = BigDecimal.valueOf(num).setScale(2, RoundingMode.HALF_UP);
                    price = bd.doubleValue();
                    if (price < Min) {
                        price = Min;
                    }
                }
                money -= price;
                count--;
                System.out.println(getName() + "抢到了" + price + "元");
                
            }
        }
        
    }
}
